<!DOCTYPE html>
<html>

<!--头文件-->
{include file='head'}
<!--bootstrap分页-->
<link rel="stylesheet" type="text/css" href="{$staticAdmin}dist/css/bootstrap.min.css">

<section class="rt_wrap content mCustomScrollbar">
 <div class="rt_content">
  <div class="page_title">
   <h2 class="fl">商品列表</h2>
   <a href="increase" class="fr top_rt_btn add_icon">添加商品</a>
  </div>
  <section class="mtb">
   <select class="select" id="sel0">
    <option value="0">选择类型</option>
    <option value="1">精品</option>
    <option value="2">新品</option>
    <option value="3">热销</option>

   </select>
   <input type="text" class="textbox textbox_225" placeholder="产品货号..." maxlength="8" id="put" />
   <input type="button" value="查询" class="group_btn" name="btn"/>
   <span id="tips"></span>
  </section>
  <table class="table" name="tb1" mySite="{$webSite}">
   <tr>
    <th class="center">缩略图</th>
    <th class="center">产品名称</th>
    <th class="center">货号</th>
    <th class="center">市场单价</th>
    <th class="center">本店单价</th>
    <th class="center">库存</th>
    <th class="center">精品</th>
    <th class="center">新品</th>
    <th class="center">热销</th>
    <th class="center">是否上架</th>
    <th class="center">操作</th>
   </tr>

   {foreach $goods as $g}
   <tr name="tr1">
    <td class="center"><img src="{$webSite}{$g['small_logo']}" width="50" height="50"/></td>
    <td class="center">{$g['goods_name']}</td>
    <td class="center">{$g['goods_card']}</td>
    <td class="center"><strong class="rmb_icon">{$g['market_price']}</strong></td>
    <td class="center"><strong class="rmb_icon">{$g['shop_price']}</strong></td>
    <!--<td class="center">{$g['weight_unit']}</td>-->
    <td class="center">{$g['stock_num']}</td>
    {if $g['is_elite'] == 1}
    <td class="center">
     <a name="isElite" title="是" class="link_icon" elite="{$g['goods_id']}">&#89;</a>
    </td>
    {else}
    <td class="center">
     <a name="isElite" title="否" class="link_icon" name="isElite" elite="{$g['goods_id']}">&#88;</a>
    </td>
    {/if}

    {if $g['is_new']}
    <td class="center">
     <a title="是" class="link_icon" name="isNew" new="{$g['goods_id']}">&#89;</a>
    </td>
    {else}
    <td class="center">
     <a title="否" class="link_icon" name="isNew" new="{$g['goods_id']}">&#88;</a>
    </td>
    {/if}

    {if $g['is_hot']}
    <td class="center">
     <a title="是" class="link_icon" name="isHot" hot="{$g['goods_id']}">&#89;</a>
    </td>
    {else}
    <td class="center">
     <a title="否" class="link_icon" name="isHot" hot="{$g['goods_id']}">&#88;</a>
    </td>
    {/if}

    {if $g['is_on_sale']}
    <td class="center">
     <a title="下架" name="receive" class="link_icon" receiver="{$g['goods_id']}">&#89;</a>
    </td>

    {else}
    <td class="center">
     <a title="上架" name="receive" class="link_icon" receiver="{$g['goods_id']}">&#88;</a>
    </td>
    {/if}

    <td class="center">
     <a title="恢复删除" class="link_icon" name="disDel" disDelete="{$g['goods_id']}">&#118;</a>
    </td>

   </tr>
   {/foreach}
  </table>

  <aside class="paging myPage">
   {$goodsPage}
  </aside>
 </div>
</section>
</body>
</html>
<script>
    //修改,标记位
    $(function () {

        //精品
        $('[name=isElite]').click(function () {

            //取值
            var $elite = $(this)[0].innerText;
            var $eliteId = $(this).attr('elite');
            var $eliteVal = null;

            //console.log($(this));
            //console.log($elite);
            //console.log($eliteId);

            //修改标记位
            if ($elite == 'Y') {
                $eliteVal = 0;
            } else if ($elite == 'X') {
                $eliteVal = 1;
            }
            //console.log($eliteVal);

            //ajax
            $.post('flag',{gid:$eliteId,elite:$eliteVal},success,'json')
        });

        //新品
        $('[name=isNew]').click(function () {

            //取值
            var $new = $(this)[0].innerText;
            var $newId = $(this).attr('new');
            var $newVal = null;

            //修改标记位
            if ($new == 'Y') {
                $newVal = 0;
            } else if ($new == 'X') {
                $newVal = 1;
            }

            //console.log($new);
            //console.log($newVal);

            //ajax
            $.post('flag',{gid:$newId,new:$newVal},success,'json')
        });


        //热销
        $('[name=isHot]').click(function () {

            //取值
            var $hot = $(this)[0].innerText;
            var $hotId = $(this).attr('hot');
            var $hotVal = null;

            //修改标记位
            if ($hot == 'Y') {
                $hotVal = 0;
            } else if ($hot == 'X') {
                $hotVal = 1;
            }

            //console.log($hot);
            //console.log($hotVal);

            //ajax
            $.post('flag',{gid:$hotId,hot:$hotVal},success,'json')
        });

        //回收(伪删除)
        $('[name=receive]').click(function () {

            //取值
            var $receive = $(this)[0].innerText;
            var $receiveId = $(this).attr('receiver');
            var $receiveVal = null;

            //修改标记位
            if ($receive == 'Y') {
                $receiveVal = 0;
            } else if ($receive == 'X') {
                $receiveVal = 1;
            }
            console.log($receive);
            console.log($receiveVal);
            //ajax
            $.post('flag',{gid:$receiveId,receive:$receiveVal},success,'json');

        });

        //恢复删除
        $('[name=disDel]').click(function () {

            var $disDelId =  $(this).attr('disDelete');
            var $disDel = 0; //回复goods_del标记位为0
console.log($disDelId);
            if ($disDelId) {

                $.post('flag',{gid:$disDelId,disDel:$disDel},success,'json');
            }
        });

        //回调函数
        function success(data)
        {
            console.log(data);
            switch (data.state) {
                //标记位修改,伪删除
                case 1:
                    alert(data.tip);
                    break;
                case 2:
                    alert(data.tip);
                    break;
            }
            //刷新当前页面
            window.location.reload();
        }
    });


    //条件查询
    $(function () {

        var fPut = false;
        var $sel = null;
        var $put = null;
        var $where1 = null;
        var $mySite = $('[name=tb1]').attr('mySite');

        //选择项
        $('#sel0').change(function () {

            //取值
            $sel = $(this).val();
            //console.log($sel);

        });

        //输入框
        $('#put').blur(function () {

            //取值
            $put = $(this).val().replace(/(^\s*)|(\s*$)/g, "");
            //console.log($put);

            //判断用户名是否为空
            if ($put.length == 0) {
                $('#tips').addClass('errorTips').html('商品编号不得为空').css('color', 'red');
                fPut = false;
                return false;
            }

            //console.log($oCard.match( /^[0-9a-zA-Z]*$/g));  //正则匹配返回数组
            //编号只能为数字,字母,或二者组合
            if ($put.length != 8 || !$put.match( /^[0-9a-zA-Z]*$/g)) {

                $('#tips').addClass('errorTips').html('编号只能为8  位数字,字母或二者组合').css('color', 'red');
                fPut = false;
                return false;
            }

            fPut = true;
            $('#tips').addClass('errorTips').html('编号合法').css('color', 'green');

        });

        //按钮点击
        $('[name=btn]').click(function () {

            //console.log(fPut);
            if (!fPut) {
                $('#tips').html('输入符合要求的条件').css('color','red');
                return false;
            }

            //处理下拉框
            if ($sel > 0) {

                if ($sel == 1) {

                    $where1 = 'elite';
                } else if ($sel == 2) {

                    $where1 = 'new';
                } else {

                    $where1 = 'hot';
                }

            } else {
                $where1 = '';
            }
            //console.log($where1);
            //console.log($put);

            //ajax
            $.post('sel',{where:$put,where1:$where1},success,'json');

        });

        //回调函数
        function success(data)
        {
            console.log(data);
            switch (data.state) {
                //存在
                case 1:
                    //alert(data.tip);
                    //查询结果
                    if (data.res) {

                        //清空
                        $('[name=tr1]').remove();
                        $('.myPage').remove();

                        //查询结果和绝对站点路径
                        var $oRes = data.res;

                        //显示查询数据
                        $('[name=tb1]').append('<tr name="tr1"></tr>');
                        $('[name=tr1]').append('<td class="center"><img src="'+ $mySite + $oRes[0]['small_logo'] +'" width="50" height="50"/></td>');
                        $('[name=tr1]').append('<td class="center">'+ $oRes[1]['goods_name'] +'</td>');
                        $('[name=tr1]').append('<td class="center">'+ $oRes[2]['goods_card'] +'</td>');
                        $('[name=tr1]').append('<td class="center"><strong class="rmb_icon">'+ $oRes[3]['market_price'] +'</strong></td>');
                        $('[name=tr1]').append('<td class="center"><strong class="rmb_icon">'+ $oRes[4]['shop_price'] +'</strong></td>');
                        $('[name=tr1]').append('<td class="center"><strong class="rmb_icon">'+ $oRes[5]['stock_num'] +'</strong></td>');

                        //精品
                        if ($oRes[6]['is_elite'] == 1) {
                            $('[name=tr1]').append('<td class="center"><a name="isElite" title="是" class="link_icon" elite="'+ $oRes[10]['goods_id'] +'">&#89;</a></td>');
                        } else {
                            $('[name=tr1]').append('<td class="center"><a name="isElite" title="否" class="link_icon" elite="'+ $oRes[10]['goods_id'] +'">&#88;</a></td>');
                        }

                        //新品
                        if ($oRes[7]['is_new'] == 1) {
                            $('[name=tr1]').append('<td class="center"><a title="是" class="link_icon" name="isNew" new="'+ $oRes[10]['goods_id'] +'">&#89;</a></td>');
                        } else {

                            $('[name=tr1]').append('<td class="center"><a title="否" class="link_icon" name="isNew" new="'+ $oRes[10]['goods_id'] +'">&#88;</a></td>');
                        }
                        //热销
                        if ($oRes[8]['is_hot'] == 1) {

                            $('[name=tr1]').append('<td class="center"><a title="是" class="link_icon" name="isHot" hot="'+ $oRes[10]['goods_id'] +'">&#89;</a></td>');
                        } else {
                            $('[name=tr1]').append('<td class="center"><a title="否" class="link_icon" name="isHot" hot="'+ $oRes[10]['goods_id'] +'">&#88;</a></td>');
                        }

                        //是否上架
                        if ($oRes[9]['is_on_sale'] == 1) {

                            $('[name=tr1]').append('<td class="center"><a title="下架" name="receive" class="link_icon" receiver="'+ $oRes[9]['goods_id'] +'">&#89;</a></td>');
                        } else {

                            $('[name=tr1]').append('<td class="center"><a title="上架" name="receive" class="link_icon" receiver="'+ $oRes[9]['goods_id'] +'">&#88;</a></td>');
                        }
                        //编辑
                        $('[name=tr1]').append('<td class="center"><a title="恢复删除" class="link_icon" name="disDel" disDelete="'+ $oRes[10]['goods_id'] +'">&#118;</a></td>');

                    }

                    break;
                //不存在
                case 2:
                    alert(data.tip);
                    fPut = false;
                    break;
            }

        }
    });


</script>
